package org.jivesoftware.smackx.filetransfer;

import java.io.InputStream;
import java.io.OutputStream;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smackx.FormField;
import org.jivesoftware.smackx.packet.DataForm;
import org.jivesoftware.smackx.packet.StreamInitiation;

public abstract class StreamNegotiator
{
  public abstract void cleanup();

  public IQ createError(String paramString1, String paramString2, String paramString3, XMPPError paramXMPPError)
  {
    IQ localIQ = FileTransferNegotiator.createIQ(paramString3, paramString2, paramString1, IQ.Type.ERROR);
    localIQ.setError(paramXMPPError);
    return localIQ;
  }

  public abstract InputStream createIncomingStream(StreamInitiation paramStreamInitiation)
    throws XMPPException;

  public StreamInitiation createInitiationAccept(StreamInitiation paramStreamInitiation, String[] paramArrayOfString)
  {
    StreamInitiation localStreamInitiation = new StreamInitiation();
    localStreamInitiation.setTo(paramStreamInitiation.getFrom());
    localStreamInitiation.setFrom(paramStreamInitiation.getTo());
    localStreamInitiation.setType(IQ.Type.RESULT);
    localStreamInitiation.setPacketID(paramStreamInitiation.getPacketID());
    DataForm localDataForm = new DataForm("submit");
    FormField localFormField = new FormField("stream-method");
    int i = paramArrayOfString.length;
    int j = 0;
    while (true)
    {
      if (j >= i)
      {
        localDataForm.addField(localFormField);
        localStreamInitiation.setFeatureNegotiationForm(localDataForm);
        return localStreamInitiation;
      }
      localFormField.addValue(paramArrayOfString[j]);
      ++j;
    }
  }

  public abstract OutputStream createOutgoingStream(String paramString1, String paramString2, String paramString3)
    throws XMPPException;

  public abstract PacketFilter getInitiationPacketFilter(String paramString1, String paramString2);

  public abstract String[] getNamespaces();

  Packet initiateIncomingStream(Connection paramConnection, StreamInitiation paramStreamInitiation)
    throws XMPPException
  {
    StreamInitiation localStreamInitiation = createInitiationAccept(paramStreamInitiation, getNamespaces());
    PacketCollector localPacketCollector = paramConnection.createPacketCollector(getInitiationPacketFilter(paramStreamInitiation.getFrom(), paramStreamInitiation.getSessionID()));
    paramConnection.sendPacket(localStreamInitiation);
    Packet localPacket = localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localPacket == null)
      throw new XMPPException("No response from file transfer initiator");
    return localPacket;
  }

  abstract InputStream negotiateIncomingStream(Packet paramPacket)
    throws XMPPException;
}